package gamebackend;

import framework.Color;
import framework.GraphicsWrapper;

/**
 * A circular explosion
 * @author Jonathan Caddey
 */
public class Explosion {
	private int my_frame;
	private int my_timeToExpand = 150;
	private int my_timeToFade = 550;
	private int my_deadFrame;
	private double my_x;
	private double my_y;
	private double my_maxDiameter = 30;
	public Explosion() {
		
	}
	public void initialize(double the_x, double the_y, double the_diameter) {
		my_x = the_x;
		my_y = the_y;
		my_frame = 0;
		my_maxDiameter = the_diameter;
		my_deadFrame = (int)(my_timeToFade * (Math.random() * .5 + 1));
	}

	public void render(final GraphicsWrapper g) {
		if (!isAlive()) {
			return;
		}
		g.saveMatrix();
		g.translate(my_x, my_y);
		final Color my_color = new Color(245, 230, 50);
		final int alpha = Math.max(0, Math.min(255, (int) (255 - 240.0 * my_frame / my_timeToFade)));
		Color color = new Color(my_color.getRed(), my_color.getGreen(), my_color.getBlue(),
				alpha);
		g.setColor(color);
		double ratio = Math.min(1, (double) my_frame / my_timeToExpand);
		int diameter = (int)(2 * my_maxDiameter /  3 * ratio + my_maxDiameter / 3);
		g.fillOval(-diameter / 2, -diameter / 2, diameter, diameter);
		g.setColor(new Color(255, 255, 255, alpha / 2));
		diameter = (int)(diameter * .7);
		g.fillOval(-diameter / 2, -diameter / 2, diameter, diameter);
		g.restoreMatrix();
	}
	
	public void step(int timeDelta, double cameraYChange) {
		if (!isAlive()) {
			return;
		}
		my_frame += timeDelta;
		my_y += cameraYChange;
		
	}
	public boolean isAlive() {
		return my_frame < my_deadFrame;
	}
}
